package com.banzhi.shop.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.data.GroupingProfitData;
import com.banzhi.shop.model.CallBack.GroupingProfitCallback;
import com.banzhi.shop.model.Impl.GroupingProfitImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/1.
 */

public class Grouping_YifaFragment extends Fragment implements GroupingProfitCallback, SwipeRefreshLayout.OnRefreshListener {
    private View view;
    private GroupingProfitImpl model;
    private int page = 1;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private LinearLayoutManager LayoutManager;
    private RelativeLayout rlnodata;
    private GroupingProfitData mGroupingProfitData;
    private GroupingProfitData.GroupingProfitBean mGroupingProfitBean;
    private List<GroupingProfitData.GroupingProfitBean.DataBean> data = new ArrayList<>();
    private List<GroupingProfitData.GroupingProfitBean.DataBean> mDataBeanList;
    private CommonAdapter<GroupingProfitData.GroupingProfitBean.DataBean> adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_grouping_profit, container, false);
        setinits(view);
        setlisteners();
        model = new GroupingProfitImpl();
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        model.getData(page+"", "1","1", this);

        return view;
    }
    private void setlisteners() {
        refreshLayout.setOnRefreshListener(this);

        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(LayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                model.getData(page+"","1","1" ,Grouping_YifaFragment.this);
            }
        });

    }

    private void setinits(View view) {
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_listview);
        refreshLayout.setColorSchemeResources(R.color.themeColor);
        refreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LayoutManager = new LinearLayoutManager(getActivity());
        LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(LayoutManager);
        rlnodata = (RelativeLayout) view.findViewById(R.id.rl_nodata);

    }

    @Override
    public void ongetDataSuccess(Object object) {
        mGroupingProfitData = (GroupingProfitData) object;
        mDataBeanList = mGroupingProfitData.getData().getData();
        if (page == 1) {
            this.data.clear();
        } else {
            if (mDataBeanList.size() == 0) {
//                ToastUtils.makeToastShort("没有更多了");
                return;
            }
        }
        this.data.addAll(mDataBeanList);

        if (this.data.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }

        if (adapter == null) {
            adapter = new CommonAdapter<GroupingProfitData.GroupingProfitBean.DataBean>(getActivity(), R.layout.item_daifa_grouping_profit, data) {
                @Override
                public void convert(ViewHolder holder, final GroupingProfitData.GroupingProfitBean.DataBean dataBean, int position) {
                    holder.setText(R.id.tv_ordernum, dataBean.getOrderno());
                    holder.getTextView(R.id.tv_fuzhi).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ShareUtils.copyLianjie(getActivity(), dataBean.getOrderno());
                        }
                    });
                    holder.setText(R.id.tv_title, dataBean.getTitle());
                    if (ASUtils.isCanUse(dataBean.getGroupsprice())) {
                        holder.setText(R.id.tv_lirun1, NumberUtils.DecimalFormat(dataBean.getGroupsprice()));
                    }
                    if (ASUtils.isCanUse(dataBean.getCreatetime())) {
                        holder.setText(R.id.tv_time, dataBean.getCreatetime());
                    }
                    holder.setText(R.id.tv_statu, dataBean.getStatus_value());

                }
            };

            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }

    }

    @Override
    public void ongetDataError(Object object) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        page = 1;
        model.getData(page+"","1","1" ,Grouping_YifaFragment.this);

    }
}
